<template>
  <div>
    <PageTitleSearch
      :title="title"
      :hotelList="hotelList"
      :currentHotel="currentHotel"
      v-model:lastMonth="lastMonth"
      :currentAcc="currentAcc"
      @hotel-change="handleSelectChange"
      @date-change="handleDateSelect"
      @acc-change="handleAccSelect"
    />
    <div class="main-content">
      <a-row :gutter="12">
        <a-col :span="12">
          <FlexCard class="flex-height" title="已售房单房能耗成本" :loading="loading" :dataList="perData" :is-center-title="true">
            <BarSingleImgGrid v-if="perData.length > 0" :dataList="perData" unit="元" formatter="dot2Formatter|0" height="250px" />
          </FlexCard>
        </a-col>
        <a-col :span="12">
          <FlexCard class="flex-height" title="能耗费用占总收入的比例" :loading="loading" :dataList="enRateData" :is-center-title="true">
            <RingImg v-if="enRateData.length > 0" :dataList="enRateData" height="300px" />
          </FlexCard>
        </a-col>
      </a-row>
      <a-row :gutter="12" style="margin-top: 12px">
        <a-col :span="8">
          <FlexCard class="flex-height" title="水消耗量" :loading="loading" :dataList="waterData" :is-center-title="true">
            <BarSingleImg v-if="waterData.length > 0" :dataList="waterData" unit="吨" formatter="dot2Formatter|0" height="300px" />
          </FlexCard>
        </a-col>
        <a-col :span="8">
          <FlexCard class="flex-height" title="电消耗量" :loading="loading" :dataList="eleData" :is-center-title="true">
            <BarSingleImg v-if="eleData.length > 0" :dataList="eleData" unit="Kwh" formatter="dot2Formatter|0" height="300px" />
          </FlexCard>
        </a-col>
        <a-col :span="8">
          <FlexCard class="flex-height" title="燃气消耗量" :loading="loading" :dataList="gasData" :is-center-title="true">
            <BarSingleImg v-if="gasData.length > 0" :dataList="gasData" unit="m³" formatter="dot2Formatter|0" height="300px" />
          </FlexCard>
        </a-col>
      </a-row>
      <a-row :gutter="12" style="margin-top: 12px">
        <a-col :span="24">
          <ReportRemarkCard
            class="remark-height"
            :loading="loading"
            v-if="reportCode"
            title="说明"
            :reportCode="reportCode"
            :reportName="reportName"
          />
        </a-col>
      </a-row>
    </div>
  </div>
</template>
<script>
  import dayjs from 'dayjs';

  import * as MathUtil from '/@/utils/MathUtil';
  import * as Color from '/@/utils/ColorDefault';
  import { defHttp } from '/@/utils/http/axios';
  import * as Constant from '/@/utils/Constant';
  import FlexCard from '@/views/applications/ui/FlexCard.vue';
  import RingImg from '@/views/applications/ui/RingImg.vue';
  import ReportRemarkCard from '@/views/applications/ui/ReportRemarkCard.vue';
  import BarSingleImg from '@/views/applications/ui/BarSingleImg.vue';
  import BarSingleImgGrid from '@/views/applications/ui/BarSingleImgGrid.vue';
  import PageTitleSearch from '@/views/applications/ui/PageTitleSearch.vue';
  const REPORT_NAME = '酒店月度能耗（项目）';
  const REPORT_CODE = 'hotel-month-single-energy';
  const APIS = {
    month: 'getHotelMonthEnergy',
    year: 'getHotelMonthAccEnergy',
  };

  export default {
    name: REPORT_NAME,

    components: {
      FlexCard,
      RingImg,
      ReportRemarkCard,
      BarSingleImg,
      BarSingleImgGrid,
      PageTitleSearch,
    },

    data() {
      return {
        title: '能耗',
        reportCode: '',
        reportName: '',
        loading: false,
        tableHigh: { x: '100%', y: 308 },
        lastMonth: dayjs().subtract(1, 'month').format('YYYYMM'),
        hotelList: [],
        accList: [
          { label: '当月', value: 'month' },
          { label: '当年', value: 'year' },
        ],
        currentHotel: {},
        currentAcc: 'month',
        waterData: [],
        eleData: [],
        gasData: [],
        enRateData: [],
        perData: [],
      };
    },

    created() {
      defHttp.get({ url: '/data/hotel/list' }, { isTransformResponse: false }).then((res) => {
        if (res.success && res.result.length > 0) {
          const dataList = res.result;
          this.hotelList = res.result.map((s) => {
            return { label: s.name, value: s.code };
          });
          console.log(this.hotelList);
          const currentHotelCode = this.$route.query.hotelCode || localStorage.getItem(Constant.STORAGE_HOTEL_NAME);
          if (currentHotelCode && dataList.filter((d) => d.code === currentHotelCode).length > 0) {
            this.currentHotel = dataList.filter((d) => d.code === currentHotelCode)[0];
          } else {
            this.currentHotel = {
              code: res.result[0].code,
              name: res.result[0].name,
            };
          }
          this.hotelSel = this.currentHotel;
          const accName = this.accList.filter((a) => a.value === this.currentAcc)[0].label;
          this.reportCode = `${REPORT_CODE}-${this.currentAcc}-${this.currentHotel.code}-${this.lastMonth}`;
          this.reportName = `${REPORT_NAME}-${accName}-${this.currentHotel.name}-${this.lastMonth}`;
          this.fetchData();
        }
      });
    },

    methods: {
      fetchData() {
        this.loading = true;
        defHttp
          .post({
            url: '/data/query',
            data: {
              labelCode: APIS[this.currentAcc],
              sqlParams: [
                {
                  key: 'code',
                  value: this.currentHotel.code,
                },
                {
                  key: 'month',
                  value: this.lastMonth,
                },
              ],
              pageIndex: 1,
              pageSize: 100,
            },
          })
          .then((res) => {
            if (res.datas.success && res.datas.data.length > 0) {
              const data = res.datas.data[0];
              this.waterData = [
                {
                  name: this.currentAcc === 'month' ? '实际' : '年累计',
                  value: data.water,
                  color: Color.COLOR_BLUE,
                },
                {
                  name: '预算',
                  value: data.waterBudget,
                  color: Color.COLOR_PURPLE,
                },
                {
                  name: '去年',
                  value: data.lWater,
                  color: Color.COLOR_WARN,
                },
              ];
              this.eleData = [
                {
                  name: this.currentAcc === 'month' ? '实际' : '年累计',
                  value: data.ele,
                  color: Color.COLOR_BLUE,
                },
                {
                  name: '预算',
                  value: data.eleBudget,
                  color: Color.COLOR_PURPLE,
                },
                {
                  name: '去年',
                  value: data.lEle,
                  color: Color.COLOR_WARN,
                },
              ];
              this.gasData = [
                {
                  name: this.currentAcc === 'month' ? '实际' : '年累计',
                  value: data.gas,
                  color: Color.COLOR_BLUE,
                },
                {
                  name: '预算',
                  value: data.gasBudget,
                  color: Color.COLOR_PURPLE,
                },
                {
                  name: '去年',
                  value: data.lGas,
                  color: Color.COLOR_WARN,
                },
              ];
              this.enRateData = [
                {
                  name: '去年',
                  value: MathUtil.safeDivide(data.lEnincome, data.lIncome),
                  color: Color.COLOR_WARN,
                },
                {
                  name: '预算',
                  value: MathUtil.safeDivide(data.enincomeBudget, data.incomeBudget),
                  color: Color.COLOR_PURPLE,
                },
                {
                  name: this.currentAcc === 'month' ? '实际' : '年累计',
                  value: MathUtil.safeDivide(data.enincome, data.income),
                  color: Color.COLOR_BLUE,
                },
              ];
              this.perData = [
                {
                  name: this.currentAcc === 'month' ? '实际' : '年累计',
                  value: MathUtil.safeDivide(data.enincome, data.sroom),
                  color: Color.COLOR_BLUE,
                },
                {
                  name: '预算',
                  value: MathUtil.safeDivide(data.enincomeBudget, data.sroomBudget),
                  color: Color.COLOR_PURPLE,
                },
                {
                  name: '去年',
                  value: MathUtil.safeDivide(data.lEnincome, data.lSroom),
                  color: Color.COLOR_WARN,
                },
              ];
            } else {
              this.waterData = [];
              this.eleData = [];
              this.gasData = [];
              this.enRateData = [];
              this.perData = [];
            }
            localStorage.setItem(Constant.STORAGE_HOTEL_NAME, this.currentHotel.code);
            this.loading = false;
          });
      },
      filterOption(input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
      },
      handleSelectChange(value) {
        this.currentHotel = {
          code: value,
          name: this.hotelList.filter((h) => h.value === value)[0].label,
        };
        const accName = this.accList.filter((a) => a.value === this.currentAcc)[0].label;
        this.reportCode = `${REPORT_CODE}-${this.currentAcc}-${this.currentHotel.code}-${this.lastMonth}`;
        this.reportName = `${REPORT_NAME}-${accName}-${this.currentHotel.name}-${this.lastMonth}`;
        this.fetchData();
      },
      handleAccSelect(value) {
        const name = this.accList.filter((h) => h.value === value)[0].label;
        this.currentAcc = value;
        this.reportCode = `${REPORT_CODE}-${value}-${this.currentHotel.code}-${this.lastMonth}`;
        this.reportName = `${REPORT_NAME}-${name}-${this.currentHotel.name}-${this.lastMonth}`;
        this.fetchData();
      },
      isMonthDisabled(current) {
        const lastMonth = dayjs().subtract(1, 'month');
        return current.isAfter(lastMonth);
      },
      handleDateSelect() {
        const name = this.accList.filter((h) => h.value === this.currentAcc)[0].label;
        this.reportCode = `${REPORT_CODE}-${this.currentAcc}-${this.currentHotel.code}-${this.lastMonth}`;
        this.reportName = `${REPORT_NAME}-${name}-${this.currentHotel.name}-${this.lastMonth}`;
        this.fetchData();
      },
    },
  };
</script>

<style lang="less" scoped>
  .main-content {
    padding: 12px 0;

    &-title {
      &-custom {
        color: #00b578;
      }
    }
  }

  .card-double {
    display: flex;
    margin: 12px 0 12px 0;
    justify-content: space-between;

    &-item {
      flex-basis: 48%;
    }
  }

  .interval-area {
    margin-bottom: 12px;
  }

  .select-title-space {
    width: 200px;
    margin-right: 20px;
  }

  ::v-deep .select-title .van-cell {
    color: hsl(217, 16%, 60%);
    font-size: 0.875rem;
    line-height: 1.25rem;
    padding: unset;
    -webkit-box-pack: end;
    -ms-flex-pack: end;
    justify-content: flex-end;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    position: relative;
    background-color: #112c4d;
  }

  ::v-deep .a-mobile-select .van-cell__value {
    color: #ffffff;
    font-size: 18px;
    font-weight: 500;
    padding-right: 0.5rem;
    text-align: center;
    display: -webkit-box;
    overflow: hidden;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
  }

  ::v-deep .a-mobile-select .van-cell .select-suffix-icon {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    width: 1.25rem;
    height: 1.25rem;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    font-size: 1rem;
    color: #8894a8;
  }
  .popup-right {
    width: auto;

    &-content {
      height: 800px;
      width: 318px;

      &-oprate {
        display: flex;
        padding: 16px;
        justify-content: center;

        &-item {
          width: 100%;
          margin-right: 10px;
        }

        &-item:last-child {
          margin-right: 0;
        }
      }
    }
  }

  @media @desktop {
    .row-content {
      display: flex;
    }
    .card-flex-all {
      flex: 1 1 calc(100% - 10px);
    }
    .card-flex {
      flex: 1 1 calc(50% - 10px);
    }
    .card-flex-70 {
      flex: 1 1 calc(66% - 10px);
    }
    .card-flex-30 {
      flex: 1 1 calc(33% - 10px);
    }
    .card-flex-60 {
      flex: 1 1 calc(60% - 10px);
    }
    .card-flex-40 {
      flex: 1 1 calc(40% - 10px);
    }

    .card-in-flex {
      display: flex;
    }

    .main-frame-desktop-header {
      padding: 1rem;
      background: #fff;
      overflow: hidden;
    }

    .a-layout-form-vertical,
    .a-layout-form-horizontal {
      width: 30rem;
      margin: 0 auto 5rem auto;
    }

    .a-layout-form-inline__warn {
      color: #ffb829;
      text-align: center;
    }

    .a-layout-form-inline .a-basic-layout-form-button button {
      min-width: 5rem;
      margin-right: 0.5rem;
    }

    .echarts {
      width: 100%;
      height: 300px;
    }
    .rate-height .echarts {
      width: 100%;
      height: 300px;
    }
    .flex-height {
      height: 368px;
    }
    .rate-height {
      height: 360px;
    }
    .remark-height {
      height: auto;
    }
  }
</style>
